<template>
  <div>
    <el-row justify="center">
      <h3>角色管理</h3>
    </el-row>
    <el-row>
        <!--       表格 -->
        <el-table :data="cutObj.records" style="width: 100%;margin-bottom: 20px " border stripe>
          <el-table-column prop="name" label="角色名称"/>
          <el-table-column label="操作">
            <template #default="scope">
              <el-button type="danger" @click="del(scope.row.id)">删除角色</el-button>
              <el-button type="primary" @click="findById(scope.row.id)">查看角色权限</el-button>
              <el-button type="primary" @click="findByIdu(scope.row.id)">修改角色权限</el-button>
            </template>
          </el-table-column>

        </el-table>
        <!--     分页   -->
        <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            background
            layout="prev, pager, next, jumper"
            :total="this.cutObj.total"
            @current-change="findByItem"
        />
        <br>
        <!--        添加-->
        <el-button type="primary"  @click="this.showAdd=true" style="margin-bottom: 20px;margin-top: 20px">添加新角色</el-button>

        <!--    个人信息弹出框    -->
        <el-dialog
            v-model="showInfo"
            title="查看角色权限" center
        >
          <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="角色名称">
              {{ roleObj.name }}
            </el-form-item>
          </el-form>
          <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="拥有权限 ">
              {{ this.str }}
            </el-form-item>
          </el-form>
          <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="showInfo = false">
          确定
        </el-button>
      </span>
          </template>
        </el-dialog>
        <!--   修改弹出框    -->
        <el-dialog
            v-model="showUpdate"
            title="查看角色权限" center
        >
          <el-form :inline="true" :model="form" class="demo-form-inline">
            <el-form-item label="角色名称">
              {{ updateObj.name }}
            </el-form-item>
          </el-form>
          <el-form :inline="true" :model="form" class="demo-form-inline">
            <el-form :inline="true" :model="f" class="demo-form-inline">
              <el-form-item label="拥有权限">
                <el-checkbox-group v-model="newArray">
                  <el-checkbox v-for="item in perArray" :label="item.id" :key="item.id" name="per" >
                    {{ item.name }}
                  </el-checkbox>
                </el-checkbox-group>
              </el-form-item>
            </el-form>
          </el-form>
          <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="update">
          确定
        </el-button>
      </span>
          </template>
        </el-dialog>

        <!--添加-->
        <el-dialog
            v-model="showAdd"
            title="修改客户信息" center
        >
          <el-form :inline="true" :model="f" class="demo-form-inline">
            <el-form-item label="角色名称">
              <el-input v-model="addObj.name"></el-input>
            </el-form-item>
          </el-form>
          <el-form :inline="true" :model="f" class="demo-form-inline">
            <el-form-item label="拥有权限">
              <el-checkbox-group v-model="addObj.permission">
                <el-checkbox v-for="item in perArray" :label="item.id" :key="item.id" name="per"
                @change="showUpdateGrade">
                  {{ item.name }}
                </el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-form>

          <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="add">
          添加
        </el-button>
      </span>
          </template>
        </el-dialog>
    </el-row>
  </div>
</template>
<script>
import axios from "axios"

export default {
  data() {
    return {
      cutObj: {},
      currentPage: '',
      pageSize: 3,
      showInfo: false,
      roleObj: {},
      formInline: "",
      showUpdate: false,
      form: "",
      updateObj: {},
      newArray:[],
      addObj: {},
      showAdd: false,
      f: "",
      perArray: [],
      perNameArray: [],
      str: "",
      currentList: [],


    }
  }, methods: {
    findByItem(pageNO) {
      this.currentPage = pageNO;
      axios.get('/project/role/findByItem', {
        params: {
          pageNO: pageNO
        }
      }).then(resp => {
        this.cutObj = resp.data;
      })
    },
    findAll() {
      axios.get('/project/permission/findAll').then(resp => {
            this.perArray = resp.data;
            // console.log(this.perArray);
          }
      )

    },
    add() {
      let formObj = new FormData;
      formObj.append("name", this.addObj.name);
      formObj.append("permission", this.addObj.permission);
      let config = {
        headers: {'Content-Type': 'multipart/form-data'}
      }
      axios.post('/project/role/add', formObj, config).then(resp => {
        let info = resp.data;
        if (info == 'ok') {
          this.showAdd = false;
          this.findByItem(1);
        }
      })
    },
    findById(roleId) {
      axios.get('/project/role/findById', {
        params: {
           roleId
        }
      }).then(resp => {
        this.showInfo = true;
        let info = resp.data;
        this.roleObj = info;
        for (let i = 0; i < info.permissionList.length; i++) {
          this.perNameArray[i] = info.permissionList[i].name;
        }
        this.str = this.perNameArray.join(',');
        this.findByItem(1);
      })

    },
    del(roleId) {
      axios.get("/project/role/del", {params: {roleId}}).then(resp => {
        if (resp.data == "ok") {
          this.findByItem(1);
        }
      })
    },
    findByIdu(roleId) {
      this.showUpdate = true;
      axios.get('/project/role/findById', {
        params: {
           roleId
        }
      }).then(resp => {
        let info = resp.data;
        this.updateObj = info;
        this.newArray = info.permissionList.map(n => n.id);
        // console.log(info.id)

      })
    },
    update(){
      let formObj = new FormData;
      formObj.append("roleId", this.updateObj.id);
      formObj.append("newArray", this.newArray);
      let config = {
        headers: {'Content-Type': 'multipart/form-data'}
      }
      axios.post('/project/role/update',formObj,config).then(
          resp=>{
            if(resp.data=='ok'){
              this.showUpdate=false;
              this.findByItem(1);
            }
          }
      )
    }
  }, created() {
    this.findByItem(1);
    this.findAll();
  }
}
</script>

<style scoped>

</style>